import 'package:flutter/material.dart';
import 'package:my_app/base/foundation/flutter_extension.dart';
import 'package:my_app/base/foundation/jd_colors.dart';
import 'package:my_app/base/foundation/jd_device.dart';
import 'package:my_app/base/router/router_util.dart';
import 'package:my_app/base/utils/loading_util.dart';
import 'package:my_app/base/widget/basic/jd_app_bar.dart';
import 'package:my_app/modules/pages/login/login_tool.dart';
import 'package:my_app/modules/pages/login/widgets/agreement_text_widget.dart';
import 'package:my_app/modules/pages/login/widgets/login_text_field.dart';
import 'package:my_app/modules/router/router_contract_page.dart';
import 'package:my_app/modules/widgets/jd_button.dart';
import 'package:my_app/modules/widgets/jd_scroll_view.dart';

// 其它手机号登录
class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final TextEditingController _phoneController = TextEditingController();
  final FocusNode _nodeText1 = FocusNode();
  bool _clickable = false;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      _phoneController.addListener(_verify);
      setState(() {
        enableBackButton = Navigator.canPop(context);
      });
    });
  }

  @override
  void dispose() {
    _phoneController.removeListener(_verify);
    _phoneController.dispose();
    super.dispose();
  }

  void _verify() {
    setState(() {
      _clickable = (_phoneController.text.length == 11);
    });
  }

  ///手机号验证
  bool isChinaPhoneLegal(String str) {
    return RegExp(
            r"^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$")
        .hasMatch(str);
  }

  // 输入验证码页面
  void getLoginCode() async {
    bool isPhone = isChinaPhoneLegal(_phoneController.text);
    if (!isPhone) {
      TDToast.showToast('请输入有效手机号');
      return;
    }
    if (!checkBoxSelected) {
      TDToast.showToast('请选勾选同意协议');
    } else {
      jumpToCodePage();
    }
  }

  jumpToCodePage() {
    RouterContractPage.instance.loginCodePage
        .push(context, params: {"phoneNo": _phoneController.text});
  }

  gotoNext() {
    if (LoginTool.isLogin()) {
      RouterUtil.popUntil(context);
      RouterContractPage.instance.tabNavigator
          .push(context, replacement: true, hideAnimation: true);
      JDLoadingTool.dismissLoading();
    } else {
      JDLoadingTool.dismissLoading();
    }
  }

  bool checkBoxSelected = false;

  bool enableBackButton = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: JDAppBar(
        showBackButton: enableBackButton,
        actionWidgets: [],
      ),
      body: JDScrollView(
        needDismissKeyBord: true,
        padding: const EdgeInsets.only(left: 36.0, right: 36.0, top: 20.0),
        children: _buildBody(),
      ),
    );
  }

  List<Widget> _buildBody() {
    return <Widget>[
      50.vGap,
      const Center(
        child: Text(
          "输入手机号码",
          style: TextStyle(
              color: JDColors.c222222,
              fontSize: 27.0,
              fontWeight: FontWeight.bold),
        ),
      ),
      60.vGap,
      LoginTextField(
        focusNode: _nodeText1,
        controller: _phoneController,
        maxLength: 11,
        keyboardType: TextInputType.phone,
        hintText: "",
      ),
      5.vGap,
      AgreementTextWidget(
        onChanged: (selected) {
          checkBoxSelected = selected;
        },
      ),
      20.vGap,
      JDButton(
        minWidth: context.width - 72,
        minHeight: 54,
        radius: 54 / 2,
        textStyle: _clickable
            ? const TextStyle(color: Color(0xFFFFFFFF), fontSize: 15)
            : const TextStyle(color: Color(0xFFFFFFFF), fontSize: 15),
        key: UniqueKey(),
        onPressed: _clickable ? getLoginCode : null,
        text: "获取验证码",
      ),
      20.vGap,
      (JDDevice.height - 440 - JDDevice.statusBarHeight).vGap,
    ];
  }
}
